package rdfparser.controller;

import java.sql.SQLException;
import java.util.List;
import rdfparser.util.ExecutionTimeCalculate;
import rdfparser.parser.FoafParser;
import rdfparser.parser.ForumParser;
import rdfparser.parser.PostParser;
import rdfparser.parser.ThreadParser;
import rdfparser.parser.UserParser;
import rdfparser.util.FileDirectoryHandler;

/**
 *
 * @author Amendra Shrestha
 */
public class RunContoller {

    private final static String BASE_DIR = "C:/Users/ITE/Downloads/2007/";
    private final static String USER_FILE = "user";
    private final static String THREAD_FILE = "threads";
    private final static String POST_FILE = "posts";
    private final static String FOAF_FILE = "foaf";
    private final static String FORUM_FILE = "forum";
    private final static String SUCCESS_MESSAGE = "\nFinished parsing all the files";
    private final static String START_MESSAGE = "*************************** START PARSING FILE ***************************";
    private final static String END_MESSAGE = "*************************** END PARSING FILE *****************************\n\n";
    private final static String ERROR_MESSAGE = "Please enter valid java run command with argument.\nE.G. "
            + "java -jar [jarfilename.jar] [argument]\n"
            + "java -jar RDFParser.jar user OR\n"
            + "java -jar RDFParser.jar threads OR\n"
            + "java -jar RDFParser.jar foaf OR\n"
            + "java -jar RDFParser.jar forum OR\n"
            + "java -jar RDFParser.jar posts OR\n";
    private static String filesList[] = null;

    /**
     * This is the main method for parsing XML files, which is invoked when
     * application is ran with passing appropriate argument. This method takes
     * the argument from console and checks the argument to get the lists of XML
     * files associated with passed argument. The http named XML files are
     * renamed to different name so that it can be parsed. After getting the
     * renamed lists of files each file is parsed by calling the appropriate
     * parser class to parse the XML files and store them in respective table in
     * MySQL tables.
     * 
     * @param args
     */
    public static void main(String[] args) throws SQLException {


        if (args.length == 1) {
            // if file category is of type 'user', get all list of files and parse each file
            if (args[0].equalsIgnoreCase(USER_FILE)) {
                long userStartTime = System.currentTimeMillis();

                FileDirectoryHandler.rootlist(BASE_DIR + USER_FILE);
                filesList = new String[FileDirectoryHandler.getList().size()];
                FileDirectoryHandler.list.toArray(filesList);
                for (int i = 0; i < filesList.length; i++) {
                    /*String fileName = filesList[i].substring(filesList[i].lastIndexOf("/") + 1,
                            filesList[i].length());*/
                    String fileName = filesList[i];
                    System.out.println("Filename: " + fileName);
                    UserParser parser = new UserParser();
                    System.out.println(START_MESSAGE);
                    parser.parseUser(fileName);
                    //parser.parseUser(BASE_DIR, USER_FILE + DELIMETER, fileName);
                    System.out.println(END_MESSAGE);
                }
                System.out.println(SUCCESS_MESSAGE);

                long userEndTime = System.currentTimeMillis();
                //calculats the execution time for user files
                System.out.println("User Execution Time: " + ExecutionTimeCalculate.returnTime(userStartTime, userEndTime) + " min");
            } // if file category is of type 'threads', get all list of files and parse each file 
            else if (args[0].equalsIgnoreCase(THREAD_FILE)) {
                long threadStartTime = System.currentTimeMillis();
                FileDirectoryHandler.rootlist(BASE_DIR + THREAD_FILE);
                filesList = new String[FileDirectoryHandler.getList().size()];
                FileDirectoryHandler.list.toArray(filesList);
                for (int i = 0; i < filesList.length; i++) {
                    /*String fileName = filesList[i].substring(
                            filesList[i].lastIndexOf("/") + 1,
                            filesList[i].length());*/
                    String fileName = filesList[i];
                    ThreadParser parser = new ThreadParser();
                    System.out.println(START_MESSAGE);
                    //parser.parseThread(BASE_DIR, THREAD_FILE + DELIMETER, fileName);
                    parser.parseThread(fileName);
                    System.out.println(END_MESSAGE);
                }

                System.out.println(SUCCESS_MESSAGE);
                long threadEndTime = System.currentTimeMillis();
                System.out.println("Thread Execution Time: " + ExecutionTimeCalculate.returnTime(threadStartTime, threadEndTime) + " min");
            }// if file category is of type 'foaf', get all list of files and parse each file 
            else if (args[0].equalsIgnoreCase(FOAF_FILE)) {
                long foafStartTime = System.currentTimeMillis();
                FileDirectoryHandler.rootlist(BASE_DIR + FOAF_FILE);
                filesList = new String[FileDirectoryHandler.getList().size()];
                FileDirectoryHandler.list.toArray(filesList);
                for (int i = 0; i < filesList.length; i++) {
                    /*String fileName = filesList[i].substring(
                            filesList[i].lastIndexOf("/") + 1,
                            filesList[i].length());*/
                    String fileName = filesList[i];
                    FoafParser parser = new FoafParser();
                    System.out.println(START_MESSAGE);
                    parser.parseFoaf(fileName);
                    System.out.println(END_MESSAGE);
                }
                System.out.println(SUCCESS_MESSAGE);
                long foafEndTime = System.currentTimeMillis();
                System.out.println("Foaf Execution Time: " + ExecutionTimeCalculate.returnTime(foafStartTime, foafEndTime) + " min");
            }// if file category is of type 'forum', get all list of files and parse each file 
            else if (args[0].equalsIgnoreCase(FORUM_FILE)) {
                long forumStartTime = System.currentTimeMillis();
                FileDirectoryHandler.rootlist(BASE_DIR + FORUM_FILE);
                filesList = new String[FileDirectoryHandler.getList().size()];
                FileDirectoryHandler.list.toArray(filesList);
                for (int i = 0; i < filesList.length; i++) {
                    /*String fileName = filesList[i].substring(
                            filesList[i].lastIndexOf("/") + 1,
                            filesList[i].length());*/
                    String fileName = filesList[i];
                    ForumParser parser = new ForumParser();
                    System.out.println(START_MESSAGE);
                    parser.parseForum(fileName);
                    System.out.println(END_MESSAGE);
                }
                System.out.println(SUCCESS_MESSAGE);
                long forumEndTime = System.currentTimeMillis();
                System.out.println("Forum Execution Time: " + ExecutionTimeCalculate.returnTime(forumStartTime, forumEndTime) + " min");
            }// if file category is of type 'posts', get all list of files and parse each file 
            else if (args[0].equalsIgnoreCase(POST_FILE)) {
                long postStartTime = System.currentTimeMillis();
                FileDirectoryHandler.rootlist(BASE_DIR + POST_FILE);
                filesList = new String[FileDirectoryHandler.getList().size()];
                FileDirectoryHandler.list.toArray(filesList);
                for (int i = 0; i < filesList.length; i++) {
                    /*String fileName = filesList[i].substring(
                            filesList[i].lastIndexOf("/") + 1,
                            filesList[i].length());*/
                    String fileName = filesList[i];
                    PostParser parser = new PostParser();
                    System.out.println(START_MESSAGE);
                    parser.parsePost(fileName);
                    System.out.println(END_MESSAGE);
                }
                System.out.println(SUCCESS_MESSAGE);

                long postEndTime = System.currentTimeMillis();
                System.out.println("Post Execution Time: " + ExecutionTimeCalculate.returnTime(postStartTime, postEndTime) + " min");
            }
        } else {
            System.out.println(ERROR_MESSAGE);
        }
    }
}
